/******************************************************/
/* Goblin's Cube - Rubik's cube game for Wii Homebrew */
/* Copyright (C) 2009 Ricardo Bueno Cordeiro          */
/* Licensed under de GNU General Public License v2.0  */
/*   See license_gplv2.txt for detalis                */
/******************************************************/

#include <gccore.h>

#include "draw.h"
#include "model.h"

#include "model_data.h"

void draw_model(int hand, f32 r, f32 g, f32 b) {
	f32 x_flip = (hand == MODEL_LEFT_HAND) ? -1.0 : 1.0;

	DR_set_hand_texture();

	GX_Begin(GX_TRIANGLES, GX_VTXFMT0, model_num_faces * 3);
	
	for (int i = 0; i < model_num_faces; i++) {
		int v1, v2, v3, n1, n2, n3, t1, t2, t3;
		
		v1 = model_faces[i][0];
		v2 = model_faces[i][1];
		v3 = model_faces[i][2];
		t1 = model_faces[i][3];
		t2 = model_faces[i][4];
		t3 = model_faces[i][5];
		n1 = model_faces[i][6];
		n2 = model_faces[i][7];
		n3 = model_faces[i][8];

		GX_Position3f32(model_vertexes[v1][0] * x_flip,  model_vertexes[v1][1], model_vertexes[v1][2]);
		GX_Normal3f32(model_normals[n1][0] * x_flip,  model_normals[n1][1], model_normals[n1][2]);
		GX_Color4u8((u8)(r * 255.0), (u8)(g * 255.0), (u8)(b * 255.0), 255);
		GX_TexCoord2f32(model_textures[t1][0], 1.0 - model_textures[t1][1]);
	
		GX_Position3f32(model_vertexes[v2][0] * x_flip,  model_vertexes[v2][1], model_vertexes[v2][2]);
		GX_Normal3f32(model_normals[n2][0] * x_flip,  model_normals[n2][1], model_normals[n2][2]);
		GX_Color4u8((u8)(r * 255.0), (u8)(g * 255.0), (u8)(b * 255.0), 255);
		GX_TexCoord2f32(model_textures[t2][0], 1.0 - model_textures[t2][1]);
	
		GX_Position3f32(model_vertexes[v3][0] * x_flip,  model_vertexes[v3][1], model_vertexes[v3][2]);
		GX_Normal3f32(model_normals[n3][0] * x_flip,  model_normals[n3][1], model_normals[n3][2]);
		GX_Color4u8((u8)(r * 255.0), (u8)(g * 255.0), (u8)(b * 255.0), 255);
		GX_TexCoord2f32(model_textures[t3][0], 1.0 - model_textures[t3][1]);
	}

	GX_End();
}
